package cn.yunhe.biz.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yunhe.biz.IRoleBiz;
import cn.yunhe.dao.RoleMapper;
import cn.yunhe.entity.Role;
import cn.yunhe.entity.RoleExample;
@Service("roleBiz")
public class RoleBizImpl implements IRoleBiz {
	@Autowired
	private RoleMapper roleMapper;

	public void setRoleMapper(RoleMapper roleMapper) {
		this.roleMapper = roleMapper;
	}

	@Override
	public List<Role> FindAllRole(String rName) {
		// TODO Auto-generated method stub
		List<Role> list = null;
		try {
			RoleExample example = new RoleExample();
			RoleExample.Criteria crita = example.createCriteria();
			if(StringUtils.isNotBlank(rName)){
				rName = "%"+ rName+"%";
			}
			if(StringUtils.isNotBlank(rName)){
				crita.andRNameLike(rName);	
			}
			list = roleMapper.selectByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Role FindRoleById(Integer rId) {
		// TODO Auto-generated method stub
		List<Role> list = null;
		try {
			RoleExample example = new RoleExample();
			RoleExample.Criteria crita = example.createCriteria();
			crita.andRIdEqualTo(rId);
			list = roleMapper.selectByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list.get(0);
	}

	@Override
	public boolean DelRoleById(Integer rId) {
		// TODO Auto-generated method stub
		Integer flag = 0;
		try {
			RoleExample example = new RoleExample();
			RoleExample.Criteria crita = example.createCriteria();
			crita.andRIdEqualTo(rId);
			flag = roleMapper.deleteByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag>0){
			return true;
		}else{
			return false;	
		}
	}

	@Override
	public Integer UpdatRoleById(Role role, Integer rId) {
		// TODO Auto-generated method stub
		Integer flag = 0;
		try {
			RoleExample example = new RoleExample();
			RoleExample.Criteria crita = example.createCriteria();
			crita.andRIdEqualTo(rId);
			flag = roleMapper.updateByExample(role, example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
}
